#!/usr/bin/env python
# -- coding: utf-8 --
"""
Copyright (c) 2018. All rights reserved.
Created by C. L. Wang on 2018/4/18

参考:
NumPy FutureWarning
https://stackoverflow.com/questions/48340392/futurewarning-conversion-of-the-second-argument-of-issubdtype-from-float-to
"""

import numpy as np

from infers.simple_mnist_infer import SimpleMnistInfer
from loaders.simple_mnist_dl import SimpleMnistDL
from models.simple_mnist_model import SimpleMnistModel
from trainers.simple_mnist_trainer import SimpleMnistTrainer
from utils.config_utils import get_train_args, process_config
from utils.logger import log


def main_train():
    """
    训练模型

    :return:
    """
    log.info('解析配置...')

    parser = None
    config = None

    # try:
    #     args, parser = get_train_args()
    #     config = process_config(args.config)
    # except Exception as e:
    #     print('[Exception] 配置无效, %s' % e)
    #     if parser:
    #         parser.print_help()
    #     print('[Exception] 参考: python main_train.py -c configs/simple_mnist_config.json')
    #     exit(0)
    config = process_config('configs/simple_mnist_config.json')

    np.random.seed(47)  # 固定随机数

    log.info('加载数据...')
    dl = SimpleMnistDL(config=config)

    log.info('构造网络...')
    model = SimpleMnistModel(config=config)

    log.info('训练网络...')
    trainer = SimpleMnistTrainer(
        model=model.model,
        data=[dl.get_train_data(), dl.get_test_data()],
        config=config)
    trainer.train()
    log.info('训练完成...')


def test_main():
    log.info('解析配置...')
    config = process_config('configs/simple_mnist_config.json')

    log.info('加载数据...')
    
    dl = SimpleMnistDL()
    test_data = np.expand_dims(dl.get_test_data()[0][0], axis=0)
    test_label = np.argmax(dl.get_test_data()[1][0])

    log.info('预测数据...')
    
    infer = SimpleMnistInfer("simple_mnist.weights.01-1.61.hdf5", config)
    infer_label = np.argmax(infer.predict(test_data))
    
    log.info('真实Label: %s, 预测Label: %s' % (test_label, infer_label))
    log.info('预测完成...')


if __name__ == '__main__':
    # main_train()
    test_main()
